import {Control} from "react-keeper";

const Actions = require('./actions');
const DB = require('../../app/db');
const {fromJS, OrderedMap} = require('immutable');
import {Boxs, Toast} from 'saltui';
import Reflux from 'reflux';
import nattyStorage from 'natty-storage';

const HBox = Boxs.HBox;
const Box = Boxs.Box;
const JdjlHelp = require('./../utils');

const storageUser = nattyStorage({
  type: 'localStorage',
  key: 'sskj-dingtalk-user',
});

module.exports = Reflux.createStore({
  listenables: [Actions],
  data: {
    rw: {},
    user: fromJS(storageUser),
    steps: []
  },

  onRwStepGet: function (params) {
    let t = this;
    DB.jdrwmx.db.get.steps(params).then(
      function (content) {
        t.data.steps = [];
        content.map((k, v)=> {
          t.data.steps.push({
            status: k.status,
            title: <HBox>
              <Box><span style={{color:'black'}}>{ k.shrName == t.data.user.get('name') ? '我' : k.shrName }</span></Box>
              <Box style={{width:window.innerWidth-180,textAlign:'right'}}><span
                style={{fontSize:'12px',fontWeight:'normal'}}>{k.shsj}</span></Box>
            </HBox>,
            description: <div><p>{k.description}</p>{k.shyj &&
            <p style={{fontSize:'15px',fontWeight:'bold',marginTop:'5px'}}>“{k.shyj}”</p>}</div>
          });
        });

        t.updateComponent();

      }, function (error) {
        Toast.hide(function () {
          alert('错误提示: ' + error.message);
        });
      });
  },

  onRwSend: function (params) {
    let t = this;
    JdjlHelp.showTost('loading', '操作中...', true, true);
    DB.jdrwmx.db.send({
      shyj: params.shyj,
      currentCode: t.data.rw.rwCode,
      userid: t.data.user.get('userid') && t.data.user.get('userid'),
      type: params.shjl,
      tjrName: t.data.rw.tjrName,
      glId: t.data.rw.glId,
      empName: t.data.user.get('name'),
      empId: t.data.rw.empId,
      rwName: t.data.rw.rwName,
      spId: t.data.rw.spId,
    }).then(
      function (content) {
        if (content.success) {
          Control.go('/dbrw');
/*          Toast.show({
            type: 'success',
            content: '操作成功!',
            autoHide: true,
            hasMask: true,
            onDidHide: function () {
              cb(athis);
            }
          });*/
        } else {
          JdjlHelp.showTost('success', '操作失败!', true, false);
          Toast.hide();
        }
        t.updateComponent();

      }, function (error) {
        //alert('错误提示: ' + error.message);
        Toast.hide();
        Control.go('/dbrw');

      });
  },

  onRwGet: function (params) {
    let t = this;
    t.data.rw = params;
    console.info('数据',t.data.rw );
            t.onRwStepGet({
              glId: t.data.rw.glId,
              groupCode: t.data.rw.groupCode,
              currentCode: t.data.rw.rwCode
            });
/*    DB.jdrwmx.db.get.mx({'spId':params.spId}).then(
      function (content) {
        t.data.rw = fromJS(content);

        Toast.hide(function () {

        });
        t.updateComponent();

      }, function (error) {
        alert('错误提示: ' + error.message);
      });*/
    t.updateComponent();
  },

  onRwRouteGet: function (params) {
    let t = this;
    DB.jdrwmx.db.get.mx({'spId':params.spId}).then(
      function (content) {
        /*t.data.rw = fromJS(content);*/
        t.data.rw = content;
        t.onRwStepGet({
          glId: t.data.rw.glId,
          groupCode: t.data.rw.groupCode,
          currentCode: t.data.rw.rwCode
        });
        Toast.hide();
        t.updateComponent();

      }, function (error) {
        alert('错误提示: ' + error.message);
      });
  },

  onSendMessageOA: function (params) {
    let t = this;
    DB.message.oa.send({
      userid: t.data.user.get('userid') && t.data.user.get('userid'),
      partyid: t.data.user.get('department') && t.data.user.get('department').join(','),
      title: params.get('empName') + '提交的' + params.get('txName'),
      text: '待办任务',
      forms: {'开始时间: ': params.get('kssj'), '结束时间: ': params.get('ywcsj')}

    }).then(
      function (content) {


      }, function (error) {
        Toast.hide(function () {
          alert('错误提示: ' + error.message);
        });
      });

  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});
